﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour {
    public static T m_instance;

    public static T Ins {
        get {
            if(m_instance == null) {
                m_instance = ObtainSingletonGameObject().AddComponentSafe<T>();
            }
            return m_instance;
        }
    }

    static GameObject m_singletonObj = null;

    private static GameObject ObtainSingletonGameObject() {
        //GameObject singletonObj = GameObject.FindGameObjectWithTag("SingletonInstances");
        if(m_singletonObj == null) {
            GameObject singletonObj = new GameObject();
            singletonObj.name = "SingletonInstances";
            //singletonObj.tag = "SingletonInstances";
            m_singletonObj = singletonObj;
            DontDestroyOnLoad(singletonObj);
            DebugUtil.LogFormat("m_singletonObj:{0}", m_singletonObj);
        }
        return m_singletonObj;

    }

    protected virtual void init() { }
}
